package cn.xunjie.ymcc.service.impl;

import cn.xunjie.ymcc.domain.Course;
import cn.xunjie.ymcc.domain.Login;
import cn.xunjie.ymcc.domain.MessageStation;
import cn.xunjie.ymcc.exception.GlobalException;
import cn.xunjie.ymcc.feign.LoginFeignClient;
import cn.xunjie.ymcc.mapper.MessageStationMapper;
import cn.xunjie.ymcc.result.JSONResult;
import cn.xunjie.ymcc.service.IMessageSmsService;
import cn.xunjie.ymcc.service.IMessageStationService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue.jiao
 * @since 2022-07-15
 */
@Service
public class MessageStationServiceImpl extends ServiceImpl<MessageStationMapper, MessageStation> implements IMessageStationService {
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Autowired
    private MessageStationMapper messageStationMapper;

    @Override
    public void saveAndUpdate(Course course) {
        JSONResult list = loginFeignClient.list();
        List<Login> logins = JSONArray.parseArray(JSON.toJSONString(list.getData()), Login.class);
        for (Login user : logins) {
            System.out.println(user);
            if(user.getType()==0){
                MessageStation messageStation = new MessageStation();
                messageStation.setTitle("课程发布啦");
                messageStation.setContent(course.getName());
                messageStation.setType("系统消息");
                
                messageStation.setSendTime(course.getOnlineTime());
                messageStation.setUserId(user.getId());
                messageStationMapper.insert(messageStation);
            }
        }

    }
}
